import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model - 服务器定义
 * @date: 2019-7-1
 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryMapIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api';
import { fetchServerList as _fetchServerList, getServerDetail as _getServerDetail, createServer as _createServer, editServer as _editServer, getServerCluster as _getServerCluster, resetPssword as _resetPssword, deleteServer as _deleteServer } from "../services/serverDefineService";
export default {
  namespace: 'serverDefine',
  state: {
    serverList: [],
    // 服务器定义列表
    typeList: [],
    // 服务器协议类别
    pagination: {},
    // 页数
    serverDetail: {},
    // 服务器明细
    serverCluster: [],
    // 服务器集群
    publicKey: '' // 公钥

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, typeList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              typeList = res.typeList;
              _context.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  typeList: typeList
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref3) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref3.call, put = _ref3.put;
              _context2.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context2.sent;

              if (!res) {
                _context2.next = 7;
                break;
              }

              _context2.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context2.abrupt("return", res);

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, getPublicKey);
    }),
    // 获取列表
    fetchServerList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServerList(_ref4, _ref5) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function fetchServerList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(_fetchServerList, parseParameters(payload));

            case 5:
              _context3.t1 = _context3.sent;
              res = (0, _context3.t0)(_context3.t1);
              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverList: res.content,
                  pagination: createPagination(res)
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchServerList);
    }),
    // 获取服务器明细
    getServerDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getServerDetail(_ref6, _ref7) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function getServerDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call, put = _ref7.put;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(_getServerDetail, payload);

            case 5:
              _context4.t1 = _context4.sent;
              res = (0, _context4.t0)(_context4.t1);
              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverDetail: res
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, getServerDetail);
    }),
    // 获取服务器集群
    getServerCluster: /*#__PURE__*/_regeneratorRuntime.mark(function getServerCluster(_ref8, _ref9) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function getServerCluster$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.t0 = getResponse;
              _context5.next = 5;
              return call(_getServerCluster, payload);

            case 5:
              _context5.t1 = _context5.sent;
              res = (0, _context5.t0)(_context5.t1);
              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  serverCluster: res.content
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, getServerCluster);
    }),
    // 新建服务器
    createServer: /*#__PURE__*/_regeneratorRuntime.mark(function createServer(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createServer$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_createServer, payload);

            case 5:
              _context6.t1 = _context6.sent;
              res = (0, _context6.t0)(_context6.t1);
              return _context6.abrupt("return", res);

            case 8:
            case "end":
              return _context6.stop();
          }
        }
      }, createServer);
    }),
    // 编辑服务器
    editServer: /*#__PURE__*/_regeneratorRuntime.mark(function editServer(_ref12, _ref13) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function editServer$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.t0 = getResponse;
              _context7.next = 5;
              return call(_editServer, payload);

            case 5:
              _context7.t1 = _context7.sent;
              res = (0, _context7.t0)(_context7.t1);
              return _context7.abrupt("return", res);

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, editServer);
    }),
    // 删除服务器
    deleteServer: /*#__PURE__*/_regeneratorRuntime.mark(function deleteServer(_ref14, _ref15) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteServer$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.t0 = getResponse;
              _context8.next = 5;
              return call(_deleteServer, payload);

            case 5:
              _context8.t1 = _context8.sent;
              res = (0, _context8.t0)(_context8.t1);
              return _context8.abrupt("return", res);

            case 8:
            case "end":
              return _context8.stop();
          }
        }
      }, deleteServer);
    }),
    // 重置密码
    resetPssword: /*#__PURE__*/_regeneratorRuntime.mark(function resetPssword(_ref16, _ref17) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function resetPssword$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.t0 = getResponse;
              _context9.next = 5;
              return call(_resetPssword, payload);

            case 5:
              _context9.t1 = _context9.sent;
              res = (0, _context9.t0)(_context9.t1);
              return _context9.abrupt("return", res);

            case 8:
            case "end":
              return _context9.stop();
          }
        }
      }, resetPssword);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref18) {
      var payload = _ref18.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};